<?php
require 'Admin/Model/Model.php';
	class NewsModel extends Model{
		function __Construct(){
			parent::__Construct();
		}
		function getNewsList(){
			$sql = "select * from news";
		    $data = $this->conn->query($sql);
			return $data;
		}
		function addNews($data){
			if(!empty($_FILES["pic"]["name"])){
  				$pic = $this->uploadfile('pic','./upload/',array('jpg','gif','png'));
  				}
			  $News = array(
					'newsname' => trim($data['newsname']), 					 
					'ntid' => $data['ntid'],
					'publishtime'=>date("YmdHis"),
					'content' => $data['content'],
					'pic'=>$pic,
				
			  	);
			  $keys = implode(',',array_keys($News));
			  $values = implode("','", array_values($News));
			  $sql="insert into news ({$keys}) VALUES ('{$values}')";
			  //执行SQL语句
			 
				$result = $this->conn->query($sql);
					return $result;
			  
		}
		function getNewsByID($id){
			$sql = "select * from news where nid=".$id;
		    $result = $this->conn->query($sql);
	        //var_dump($result);
	      	return $result;
	      	//var_dump($result);

		}
		function getnewstype($id){
      $sql = "select ntid from news where nid = '$id'";
      $result = $this->conn->query($sql);
      //var_dump($result);
      return $result;
    }

		function update($data){
			$news = array(
			'ntid' => $data['ntid'],
			'newsname' => $data['newsname'],
		  // $pic = './upload/'.$data['pic'],
		  'publishtime' => $data['publishtime'],		
		    'content' => $data['content']
			);
			if(!empty($_FILES["pic"]["name"])){
  				$pic = $this->uploadfile('pic','./upload/',array('jpg','gif','png'));
  				$news['pic'] = $pic;
  				}
			  
			 		  
			 $sets = '';
			  foreach ($news as $key=>$val){
			        $sets.=$key."='".$val."',";
			    }
			    $sets=rtrim($sets,','); //去掉SQL里的最后一个逗号
			   
			    $sql="UPDATE news SET {$sets} where nid='".$data['nid']."'";
			
			   
			  //执行SQL语句
				$result = $this->conn->query($sql);
			
					return $result;
		}
		function delNews($id){
			$sql1 = "select * from news where nid=".$id;
			$data1 = $this->conn->query($sql1);
			$filepath="./upload/".$data1['pic'];
			if(file_exists($filepath))
				{
					unlink($filepath);
					echo "头像删除成功".'<br>';
				}	
			else
				echo "头像删除失败".'<br>';
			$sql = "delete from news where nid=".$id;
			
			$result = $this->conn->query($sql);
			return $result;
		}
		function Search($data){
       // var_dump($data);
			$sql="select * from news where newsname like '%".$data."%'";
		//	echo $sql;  
			$result = $this->conn->query($sql);
			//var_dump($result);
			return $result;
		}

	}
?>



